//
//  HSDetailsViewController.swift
//  HomeSchoolDemo
//
//  Created by rimi on 16/10/12.
//  Copyright © 2016年 黄华强. All rights reserved.
//

import UIKit
import SVProgressHUD



class HSDetailsViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    
    var id: Int = 0
    var dataArray:[HSHomeWorkTableModel] = []
    var objectiveTopicButton:UIButton!
    var subjectiveTopicButton:UIButton!
    
    var objectiveTopicTableView: UITableView!
    var subjectiveTopicTableView: UITableView!
    
    let detailsListVM = HSHWDetailsListViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        
        
        setupUI()
        loadData()
        objectiveTopicTableView.estimatedRowHeight = 200
        subjectiveTopicTableView.estimatedRowHeight = 200
    }
    
    func setupUI() {
        objectiveTopicButton = UIButton(frame: CGRect(x: 0, y: 64, width: screenW/2, height: 44))
        objectiveTopicButton.backgroundColor = #colorLiteral(red: 0.2588235438, green: 0.7568627596, blue: 0.9686274529, alpha: 1)
        objectiveTopicButton.setTitle("客观题", for: .normal)
        objectiveTopicButton.addTarget(self, action: #selector(changeUI), for: .touchUpInside)
        view.addSubview(objectiveTopicButton)
        objectiveTopicTableView = UITableView(frame: CGRect(x: 0, y: 108, width: screenW, height: screenH - 108))
        objectiveTopicTableView.backgroundColor = #colorLiteral(red: 0.9764705896, green: 0.850980401, blue: 0.5490196347, alpha: 1)
        objectiveTopicTableView.dataSource = self
        objectiveTopicTableView.delegate = self
        view.addSubview(objectiveTopicTableView)
        
        subjectiveTopicButton = UIButton(frame: CGRect(x: screenW/2, y: 64, width: screenW/2, height: 44))
        subjectiveTopicButton.backgroundColor = #colorLiteral(red: 0.4, green: 0.6666666667, blue: 0.9333333333, alpha: 1)
        subjectiveTopicButton.setTitle("主观题", for: .normal)
        subjectiveTopicButton.setTitleColor(#colorLiteral(red: 0.2980392157, green: 0.2980392157, blue: 0.2980392157, alpha: 1), for: .normal)
        subjectiveTopicButton.addTarget(self, action: #selector(changeUI), for: .touchUpInside)
        view.addSubview(subjectiveTopicButton)
        subjectiveTopicTableView = UITableView(frame: CGRect(x: 0, y: 108, width: screenW, height: screenH - 108))
        subjectiveTopicTableView.backgroundColor = #colorLiteral(red: 0.9372549057, green: 0.3490196168, blue: 0.1921568662, alpha: 1)
        subjectiveTopicTableView.dataSource = self
        subjectiveTopicTableView.delegate = self
        subjectiveTopicTableView.isHidden = true
        view.addSubview(subjectiveTopicTableView)
        
        
        
                
        
        
        objectiveTopicTableView.register(HSObjectiveTableViewCell.self, forCellReuseIdentifier: "objectiveCell")
        subjectiveTopicTableView.register(HSSubjectiveTableViewCell.self, forCellReuseIdentifier: "subjectiveCell")
        
    }
    
    func changeUI() {
        if objectiveTopicButton.backgroundColor == #colorLiteral(red: 0.2588235438, green: 0.7568627596, blue: 0.9686274529, alpha: 1) {
            objectiveTopicButton.backgroundColor = #colorLiteral(red: 0.4, green: 0.6666666667, blue: 0.9333333333, alpha: 1)
            objectiveTopicButton.setTitleColor(#colorLiteral(red: 0.2980392157, green: 0.2980392157, blue: 0.2980392157, alpha: 1), for: .normal)
            
            objectiveTopicTableView.isHidden = true
            subjectiveTopicTableView.isHidden = false
            subjectiveTopicButton.backgroundColor = #colorLiteral(red: 0.2588235438, green: 0.7568627596, blue: 0.9686274529, alpha: 1)
            subjectiveTopicButton.setTitleColor(#colorLiteral(red: 1, green: 1, blue: 1, alpha: 1), for: .normal)
        } else {
            subjectiveTopicButton.backgroundColor = #colorLiteral(red: 0.4, green: 0.6666666667, blue: 0.9333333333, alpha: 1)
            subjectiveTopicButton.setTitleColor(#colorLiteral(red: 0.2980392157, green: 0.2980392157, blue: 0.2980392157, alpha: 1), for: .normal)
            objectiveTopicTableView.isHidden = false
            subjectiveTopicTableView.isHidden = true
            objectiveTopicButton.backgroundColor = #colorLiteral(red: 0.2588235438, green: 0.7568627596, blue: 0.9686274529, alpha: 1)
            objectiveTopicButton.setTitleColor(#colorLiteral(red: 1, green: 1, blue: 1, alpha: 1), for: .normal)
            
        }
        
    }
    
    
    
    func loadData() {
        
        detailsListVM.requestDetails(hwID: id) {[weak self] (error, isReloadData) in
            
            if isReloadData {
                self?.objectiveTopicTableView.reloadData()
                self?.subjectiveTopicTableView.reloadData()
            }
            
        }
        
    }
    
    
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if tableView == objectiveTopicTableView {
            return detailsListVM.detailsArray[0][indexPath.row].cellHeight
        } else {
            return detailsListVM.detailsArray[1][indexPath.row].cellHeight
        }
    }
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if tableView == objectiveTopicTableView {
            
            return detailsListVM.detailsArray[0].count
        } else {
            return detailsListVM.detailsArray[1].count
        }
        
        
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if tableView == objectiveTopicTableView {
            let cell = tableView.dequeueReusableCell(withIdentifier: "objectiveCell", for: indexPath) as! HSObjectiveTableViewCell
         
            cell.model = detailsListVM.detailsArray[0][indexPath.row]
            
            cell.selectionStyle = .none
            return cell
            
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "subjectiveCell", for: indexPath) as! HSSubjectiveTableViewCell
            
            cell.model = detailsListVM.detailsArray[1][indexPath.row]
            
            cell.selectionStyle = .none
            return cell
        }
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
}
